package com.learn.smart.framework.proxy;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import net.sf.cglib.proxy.MethodProxy;

public class ProxyChain {
	
	private Class<?> targetClass;
	private Object targetObject;
	private Method targetMethod;
	private MethodProxy methodProxy;
	private Object[] methodParams;
	
	private List<Proxy> proxyList = new ArrayList<Proxy>();
	
	private int proxyIndex=0;

	public ProxyChain(Class<?> targetClass, Object targetObject, Method targetMethod,MethodProxy methodProxy, Object[] methodParams, List<Proxy> proxyList) {
		this.targetClass = targetClass;
		this.targetObject = targetObject;
		this.targetMethod = targetMethod;
		this.methodProxy = methodProxy;
		this.methodParams = methodParams;
		this.proxyList = proxyList;
	}

	public Class<?> getTargetClass() {
		return targetClass;
	}

	public Method getTargetMethod() {
		return targetMethod;
	}

	public Object[] getMethodParams() {
		return methodParams;
	}
	
	public Object doProxyChain() throws Throwable{
		Object methodResult;
		if(proxyIndex<proxyList.size()){
			methodResult = proxyList.get(proxyIndex++).doProxy(this);
		}else{
			methodResult = methodProxy.invoke(targetObject, methodParams);
		}
		return methodResult;
	}
	

}
